<template>
  <view class="my-page">
    <!-- 顶部欢迎栏（含头像、登录/用户名、统计） -->
    <view class="top-bar">
      <view class="avatar-wrap">
        <!-- 头像（替换为项目内静态资源） -->
        <image class="avatar" src="/static/头像.jpeg" mode="widthFix"></image>
        <view class="welcome-group">
          <!-- 核心修改：根据登录状态动态显示 -->
          <text class="welcome-text">HI~{{ isLogin ? userName : '欢迎回来' }}</text>
          
          <!-- 未登录：显示登录按钮；已登录：显示退出登录按钮 -->
          <button 
            v-if="!isLogin" 
            class="login-btn" 
            @click="goToLogin"
          >
            登录
          </button>
          <button 
            v-else 
            class="logout-btn" 
            @click="logout"
          >
            退出登录
          </button>
        </view>
      </view>
      <!-- 关注/话题统计 -->
      <view class="stats-bar">
        <text class="stat-item">关注{{ followCount }}</text>
        <text class="divider">|</text>
        <text class="stat-item">话题{{ topicCount }}</text>
      </view>
    </view>

    <!-- 功能按钮区（收藏/历史/设置） -->
    <view class="func-buttons">
      <view class="func-item" @click="toCollect">
        <image class="func-icon" src="/static/收藏.png" mode="widthFix"></image>
        <text class="func-text">收藏</text>
      </view>
      <view class="func-item" @click="toHistory">
        <image class="func-icon" src="/static/历史.png" mode="widthFix"></image>
        <text class="func-text">历史</text>
      </view>
      <view class="func-item" @click="toSetting">
        <image class="func-icon" src="/static/设置.png" mode="widthFix"></image>
        <text class="func-text">设置</text>
      </view>
    </view>

    <!-- 广告横幅 -->
    <view class="ad-banner" @click="toAd">
      <image class="ad-img" src="/static/广告.jpg" mode="widthFix"></image>
    </view>

    <!-- 发布引导区 -->
    <view class="publish-guide">
      <view class="publish-icons">
        <image class="publish-icon" src="/static/icon-publish-post.png" mode="widthFix"></image>
        <image class="publish-icon" src="/static/icon-publish-img.png" mode="widthFix"></image>
        <image class="publish-icon" src="/static/icon-publish-link.png" mode="widthFix"></image>
      </view>
      <text class="guide-text">已有{{ publishCount }}人在这里发布身边的新鲜事</text>
      <button class="publish-btn" @click="toPublish">我也要发布</button>
      <navigator class="browse-link" url="/pages/tabBar/NewsPage/NewsPage">先去逛逛 ></navigator>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      followCount: 3,       // 关注数
      topicCount: 0,        // 话题数
      publishCount: "385,893", // 发布人数
      isLogin: false,       // 登录状态
      userName: ''          // 登录后的用户名
    };
  },
  // 核心：页面显示时读取登录状态
  onShow() {
    this.checkLoginStatus();
  },
  methods: {
    // 检查登录状态（读取本地存储）
    checkLoginStatus() {
      const userInfo = uni.getStorageSync('userInfo');
      if (userInfo && userInfo.isLogin) {
        this.isLogin = true;
        this.userName = userInfo.username; // 赋值用户名
      } else {
        this.isLogin = false;
        this.userName = '';
      }
    },
    // 跳转到登录页面
    goToLogin() {
      uni.navigateTo({
        url: '/pages/index/auth/login' // 对应你的登录页路径
      });
    },
    // 退出登录
    logout() {
      uni.showModal({
        title: '提示',
        content: '确定要退出登录吗？',
        success: (res) => {
          if (res.confirm) {
            // 清空本地存储的登录状态
            uni.removeStorageSync('userInfo');
            // 刷新登录状态
            this.checkLoginStatus();
            uni.showToast({ title: '退出成功', icon: 'success' });
          }
        }
      });
    },
    // 功能按钮跳转（可扩展对应页面）
    toCollect() {
      uni.showToast({ title: "跳转至收藏页", icon: "none" });
    },
    toHistory() {
      uni.showToast({ title: "跳转至历史页", icon: "none" });
    },
    toSetting() {
      uni.showToast({ title: "跳转至设置页", icon: "none" });
    },
    // 广告跳转
    toAd() {
      uni.showToast({ title: "跳转至广告活动页", icon: "none" });
    },
    // 发布跳转
    toPublish() {
      uni.showToast({ title: "跳转至发布页", icon: "none" });
    }
  }
};
</script>

<style scoped>
/* 页面根容器 */
.my-page {
  background-color: #f5f7fa;
  min-height: 100vh;
  padding: 0 10rpx;
  box-sizing: border-box;
}

/* 顶部欢迎栏 */
.top-bar {
  background-image: url("/static/背景图片.jpg"); /* 替换为截图风格的背景图 */
  background-size: cover;
  background-position: center;
  padding: 25rpx 20rpx;
  border-radius: 0 0 20rpx 20rpx;
  color: #fff;
  margin-bottom: 20rpx;
}
.avatar-wrap {
  display: flex;
  align-items: center;
  gap: 15rpx;
  margin-bottom: 15rpx;
}
.avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  border: 2rpx solid #fff;
}
.welcome-group {
  display: flex;
  flex-direction: column;
  gap: 8rpx;
}
.welcome-text {
  font-size: 32rpx;
  font-weight: 500;
}
/* 登录按钮样式 */
.login-btn {
  background-color: #ff4500;
  color: #fff;
  font-size: 28rpx;
  padding: 6rpx 20rpx;
  border-radius: 20rpx;
  border: none;
  width: fit-content;
  transition: all 0.2s ease;
}
.login-btn:active {
  background-color: #e03e00;
  transform: scale(0.95);
}
/* 退出登录按钮样式 */
.logout-btn {
  background-color: #666;
  color: #fff;
  font-size: 28rpx;
  padding: 6rpx 20rpx;
  border-radius: 20rpx;
  border: none;
  width: fit-content;
  transition: all 0.2s ease;
}
.logout-btn:active {
  background-color: #444;
  transform: scale(0.95);
}
.stats-bar {
  display: flex;
  justify-content: center;
  gap: 25rpx;
  font-size: 28rpx;
  opacity: 0.9;
}
.divider {
  opacity: 0.7;
}

/* 功能按钮区 */
.func-buttons {
  display: flex;
  justify-content: space-around;
  padding: 20rpx 0;
  background-color: #fff;
  border-radius: 15rpx;
  margin-bottom: 20rpx;
}
.func-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8rpx;
  cursor: pointer;
}
.func-icon {
  width: 50rpx;
  height: 50rpx;
}
.func-text {
  font-size: 26rpx;
  color: #666;
}

/* 广告横幅 */
.ad-banner {
  width: 100%;
  border-radius: 15rpx;
  overflow: hidden;
  margin-bottom: 20rpx;
}
.ad-img {
  width: 100%;
  height: auto;
}

/* 发布引导区 */
.publish-guide {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 30rpx 20rpx;
  background-color: #fff;
  border-radius: 15rpx;
  gap: 18rpx;
}
.publish-icons {
  display: flex;
  gap: 40rpx;
}
.publish-icon {
  width: 60rpx;
  height: 60rpx;
}
.guide-text {
  font-size: 28rpx;
  color: #999;
  text-align: center;
}
.publish-btn {
  background-color: #ff4500;
  color: #fff;
  font-size: 30rpx;
  padding: 12rpx 45rpx;
  border-radius: 30rpx;
  border: none;
}
.browse-link {
  font-size: 26rpx;
  color: #4096ff;
  text-decoration: none;
}
</style>